import tiyDetailData from "../chatMessageDetail/chat_message_detail_data"

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalClear.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalClear.isLoading = true;
  });
}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.searchParams.userId = "";
  tiyData.tParams.searchParams.userName ="";
  tiyData.tParams.searchParams.searchAll = false;
  tiyData.tParams.searchParams.onlyUnread = true;
  tiyData.tParams.searchParams.assigned = "";
  tiyData.tParams.searchParams.messageStatus = '';
  tiyData.tParams.searchParams.tag = null;
  tiyData.tParams.searchParams.sortBy = "createTime";
  tiyData.tData.messageList.splice(0);
  self.unAssignedMessage = 0;
  self.isCheckedAllMessage = false;
  self.tags.splice(0);
  self.tags.push(...tiyDetailData.tData.tagList);
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  // self.$Spin.show();
  if(tiyData.tParams.selectedEmployee===undefined){
    tiyData.tParams.selectedEmployee='';
  }
  self.isLoading = true;
  let params = {
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "userName":tiyData.tParams.searchParams.userName,
    // "selectAll":tiyData.tParams.searchParams.searchAll ,
    "selectAll":self.$authority(self.tRole.COMMON_1_100),
    "onlyUnread":tiyData.tParams.searchParams.onlyUnread,
    "assigned":tiyData.tParams.searchParams.assigned=='undefined'?'':tiyData.tParams.searchParams.assigned,
    "sortBy":tiyData.tParams.searchParams.sortBy,
    "messageStatus": tiyData.tParams.searchParams.messageStatus,
    "tag": tiyData.tParams.searchParams.tag,
    "searchUnReply": tiyData.tParams.searchParams.searchUnReply,
    "employeeId":tiyData.tParams.selectedEmployee,
  };
  if(tiyData.tParams.searchParams.searchUnReply) {
    params.timeLimit = 24;
  }
  self.$apiChat.chatMessageList(params).then(function (response) {
    self.$Spin.hide();
    tiyData.tData.messageList.splice(0, tiyData.tData.messageList.length);
    self.unAssignedMessage = 0;
    self.isCheckedAllMessage = false;
    if (response.success) {
      let messagePage = response.result;
      tiyData.tPage.total = messagePage.total;
      tiyData.tPage.stepNumber = messagePage.pageSize;
      messagePage.list.forEach(
        function (item) {
          tiyData.tData.messageList.push(item);
          if(!item.employee_id){
            self.unAssignedMessage ++;
          }
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
    self.refreshTime = 60;
    self.isLoading = false;
    self.messageSelected.splice(0,self.messageSelected.length);
    //防止进入死循环
    // if(!self.refresh && self.timer!=null && self.timer!=''){
    //   clearInterval(self.timer);
    //   self.timer = '';
    // }
  });
};


/**
 * 发送消息推送
 * @param self
 * @param tiyData
 */
export const sendMessage = (self, tiyData) => {


  tiyData.tStatus.modalMessageSend.isShow=true;
  // if(tiyData.tParams.messageSendParam.userId == '' && tiyData.tParams.messageSendParam.token == ''){
  //   self.$Modal.warning({
  //     title:"提示",
  //     content:"请输入用户id或者用户token"
  //   });
  //   return
  // }

  // self.$refs["sendMessageForm"].validate((valid) => {
  //   if (valid) {
  //     self.$Spin.show();
  //     self.$apiUser.sendMessage({
  //       "userId":tiyData.tParams.messageSendParam.userId,
  //       "userName":tiyData.tParams.messageSendParam.userName,
  //       "token":tiyData.tParams.messageSendParam.token,
  //       "content":tiyData.tParams.messageSendParam.content,
  //       "title":tiyData.tParams.messageSendParam.title,
  //     }).then(function (response) {
  //       self.$Spin.hide();
  //       if (response.success) {
  //         self.$Message.success("发送成功");
  //         tiyData.tStatus.modalMessageSend.isShow = false;
  //       } else {
  //         self.$Message.error("发送失败！" + response.errorMessage);
  //       }
  //     });
  //   }
  // })

};

// /**
//  * 消息推送详情
//  * @param self
//  * @param tiyData
//  * @returns {Array}
//  */
// export const messageDetail = (self, tiyData) => {
//   self.$Spin.show();
//   self.$apiUser.messageDetail({
//     "id":tiyData.tParams.messageDetailParam.messageId,
//   }).then(function (response) {
//     self.$Spin.hide();
//     if (response.success) {
//       let message = response.result;
//       tiyData.tStatus.modalMessageDetail.message.id = message.id;
//       tiyData.tStatus.modalMessageDetail.message.userId = message.userId;
//       tiyData.tStatus.modalMessageDetail.message.messageAccount = message.messageAccount;
//       tiyData.tStatus.modalMessageDetail.message.user.name = message.user.name;
//       tiyData.tStatus.modalMessageDetail.message.title = message.title;
//       tiyData.tStatus.modalMessageDetail.message.content = message.content;
//       tiyData.tStatus.modalMessageDetail.isShow = true;
//     } else {
//       self.$Message.error(response.errorMessage);
//     }
//   });
// };

/**
 * 推送消息人的查询
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const msgAccountList = (self, tiyData) => {
  if(tiyData.tParams.messageAccountParam.firstName == ''){
    self.$Modal.warning({
      title:"提示",
      content:"请输入用户名或用户编号"
    });
    return;
  }
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.accountList);
  self.$apiChat.msgAccountList({
    "userName":tiyData.tParams.messageAccountParam.firstName
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let messageAccount = response.result;
      messageAccount.list.forEach(function (item) {
        tiyData.tData.accountList.push(item)
      });
      if(tiyData.tData.accountList.length == 0){
        self.$Message.success("查询没有结果");
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 查询负责人列表
 * @param self
 * @param tiyData
 */
export const getEmployee = (self, tiyData) => {
  // self.$Spin.show();
  tiyData.tParams.selectedEmployee = ''; //清空之前选择的员工
  self.$apiRole.getEmployee({"pageSize": 500, "pageNum": 1}).then(function (response) {
    if (response.success) {
      let employeeList = response.result.list;
      tiyData.tData.employeeList.splice(0, tiyData.tData.employeeList.length);
      employeeList.forEach(
        function (item) {
          tiyData.tData.employeeList.push(item);
        });
    } else {
      self.$Message.error(response.errorMessage);
    }
    // self.$Spin.hide();
  });
};

//查询超过24小时未回复的消息
export const searchUnReply = (self, tiyData) => {
  self.isLoading = true;
  let params = {
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "timeLimit": 24,
  }
  self.$apiChat.searchUnReply(params).then(response => {
    self.$Spin.hide();
    tiyData.tData.messageList.splice(0, tiyData.tData.messageList.length);
    self.unAssignedMessage = 0;
    self.isCheckedAllMessage = false;
    if(response.success) {
      let messagePage = response.result;
      tiyData.tPage.total = messagePage.total;
      tiyData.tPage.stepNumber = messagePage.pageSize;
      messagePage.list.forEach(
        function (item) {
          tiyData.tData.messageList.push(item);
          if(!item.employee_id){
            self.unAssignedMessage ++;
          }
        }
      );
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

export const getCustomerService = (self, tiyData) => {
  tiyData.tData.customerService.employeeId = "";
  tiyData.tData.customerService.name = "";
  tiyData.tData.customerService.state = null;
  tiyData.tData.customerService.avator = "";
  tiyData.tData.customerService.createtime = null;
  self.$Spin.show();
  self.$apiChat.getSelfCustomerService().then(response => {
    self.$Spin.hide();
    if(response.success) {
      if(response.result == null) {
        self.$Message.error("未查询到您的Live Chat 座席信息");
      }else{
        tiyData.tData.customerService.employeeId = response.result.employeeId;
        tiyData.tData.customerService.name = response.result.name;
        tiyData.tData.customerService.state = response.result.state;
        tiyData.tData.customerService.avator = response.result.avator;
        tiyData.tData.customerService.createtime = response.result.createtime;
      }
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("查询Live Chat 座席信息失败!");
  });
}

//Live Chat座席上线
export const onlineCustomerService = (self, tiyData) => {
  self.$Spin.show();
  self.$apiChat.onlineSelfCustomerService().then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("您的Live Chat 座席身份已上线！");
      getCustomerService(self, tiyData);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("Live Chat 座席上线失败!");
  })
}

//Live Chat座席上线
export const offlineCustomerService = (self, tiyData) => {
  self.$Spin.show();
  self.$apiChat.offlineSelfCustomerService().then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("您的Live Chat 座席身份已下线！");
      getCustomerService(self, tiyData);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("Live Chat 座席下线失败!");
  })
}
